<?php

declare(strict_types=1);

namespace App\Service\Client;


use App\Amqp\DelayProducer;
use App\Amqp\Producer\DeleteTmpFileProducer;
use App\Constants\DefaultValue;
use App\Dao\ConfigBannerDao;
use App\Dao\ConfigDiscoverDao;
use App\Dao\ConfigOtherDao;
use App\Dao\TaskTypeDao;
use App\Model\ConfigBanner;
use App\Utils\Log;
use App\Utils\Upload;
use Hyperf\Amqp\Result;
use Hyperf\Di\Annotation\Inject;
use App\Constants\Model\ConfigBanner as ConfigBannerConstants;

class CommonService extends BaseService
{
    /**
     * @Inject()
     * @var DelayProducer
     */
    protected $delayProducer;

    /**
     * @Inject()
     * @var ConfigBannerDao
     */
    protected $configBannerDao;

    /**
     * @Inject()
     * @var ConfigDiscoverDao
     */
    protected $configDiscoverDao;

    /**
     * @Inject()
     * @var ConfigOtherDao
     */
    protected $configOtherDao;

    /**
     * @Inject()
     * @var TaskTypeDao
     */
    protected $taskTypeDao;

    /**
     * 获取客服电话
     * @return array
     */
    public function servicePhone(): array
    {
        $phone = $this->configOtherDao->getServicePhone($this->cooperativeInfo()->id);
        return ['phone' => $phone];
    }

    /**
     * 任务类型
     * @return array|array[]
     */
    public function taskType(): array
    {
        $taskType = $this->taskTypeDao->where(['cooperative_id' => $this->userInfo()->cooperative_id])->get();
        return [
            'taskType' => $taskType,
        ];
    }


    /**
     * banner列表
     * @return array|array[]
     */
    public function banner(): array
    {
        /** @var ConfigBanner $banner */
        $banner = $this->configBannerDao
            ->where(
                [
                    'cooperative_id' => $this->userInfo()->cooperative_id,
                    'status' => ConfigBannerConstants::STATUS_UNBLOCK
                ]
            )
            ->select(['id', 'type', 'ext', 'image'])
            ->orderBy('rank', 'asc')
            ->get();
        if (!$banner) {
            $banner = [];
        }

        return [
            'banner' => $banner
        ];
    }


    /**
     * 发现页
     * @param array $data
     * @return array|array[]
     */
    public function discover(array $data): array
    {
        $perPage = isset($data['perPage']) ? (int)$data['perPage'] : DefaultValue::PER_PAGE_DEFAULT;

        $page = $this->configDiscoverDao
            ->paginate(
                ['cooperative_id' => $this->userInfo()->cooperative_id],
                $perPage,
                ['*'],
                ['orderBy' => ['rank', 'asc']]
            );

        return [
            'perPage' => $page->perPage(),//每页条数
            'currentPage' => $page->currentPage(),//当前页
            'total' => $page->total(),//数据总数
            'hasMorePages' => $page->hasMorePages(),//是否还有更多页
            'data' => $page->items(),//数据
        ];
    }


    /**
     * 抽成比例
     * @return array
     */
    public function commissionPercent()
    {
        $commissionPercent = $this->configOtherDao->getCommissionPercent($this->userInfo()->cooperative_id);
        return [
            'commissionPercent' => $commissionPercent
        ];
    }

    /**
     * @param array $data
     * @return array
     * @throws \Throwable
     */
    public function uploadImage(array $data)
    {
        $upload = new Upload(
            [
                'path' => '/tmp',
                'allowSuffix' => $this->imageSuffix(),
                'allowMime' => $this->imageMime(),
                'postfix' => $this->timeRandom()
            ]
        );

        $filePath = $upload->uploadFile($data['image']);

        //加入延时队列，到期自动删除(1小时)
        $message = new DeleteTmpFileProducer(['filePath' => $filePath]);
        $this->delayProducer->produce($message, DefaultValue::DELETE_TMP_FILE_TIME);

        return [
            'host' => env('APP_HOST'),
            'filePath' => $filePath
        ];
    }


    public function deleteTmpFile(array $data): string
    {
        try {
            $this->deleteFile($data['filePath']);
        } catch (\Throwable $e) {
            return Result::REQUEUE;
        }
        return Result::ACK;
    }

}